#include "../inc/Drawable.h"

void Drawable::applyRotation() 
{ 
     glRotatef( this->getRotation().x, 1, 0, 0 );
     glRotatef( this->getRotation().y, 0, 1, 0 );
     glRotatef( this->getRotation().z, 0, 0, 1 );
}

void Drawable::applyTranslation()
{ 
     glTranslatef( this->getPos().x, this->getPos().y, this->getPos().z );
}

void Drawable::beginRender()
{
     this->mMat.initMaterial();
     
     glPushMatrix();
     
     this->applyTranslation();
     this->applyRotation();
}

void Drawable::endRender()
{
     glPopMatrix();
     
     this->mMat.finishMaterial();
}

void Drawable::rotate ( CoordF r )
{
     this->cRot += r;
     
     // Corrige amplitude de valores
     while (this->cRot.x < -360.0) this->cRot.x += 360.0;
     while (this->cRot.y < -360.0) this->cRot.y += 360.0;
     while (this->cRot.z < -360.0) this->cRot.z += 360.0;
     while (this->cRot.x > 360.0) this->cRot.x -= 360.0;
     while (this->cRot.y > 360.0) this->cRot.y -= 360.0;
     while (this->cRot.z > 360.0) this->cRot.z -= 360.0;
     
}

void Drawable::draw()
{
     this->beginRender();
     
     this->render();
     
     this->endRender();
}
     
